package com.jimikongjian.service;

import com.jimikongjian.models.Comment;
import com.jimikongjian.models.Message;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.service.base.IArticleService;
import com.jimikongjian.service.base.ICommentService;
import com.jimikongjian.service.base.IUnreadCommentService;
import com.jimikongjian.service.base.IUserService;
import com.jimikongjian.service.dao.impl.ICommentImpl;
import com.jimikongjian.service.message.ResponseMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by fuxiuyang on 17/3/13.
 */
@Service("commentService")
public class CommentService implements ICommentService{


    @Autowired
    @Qualifier("responseMessageService")
    private ResponseMessageService responseMessageService;


    @Autowired
    @Qualifier("iCommentImpl")
    private ICommentImpl iCommentImpl;


    @Autowired
    @Qualifier("userService")
    private IUserService userService;


    @Autowired
    @Qualifier("articleService")
    private IArticleService articleService;


    @Autowired
    @Qualifier("unreadCommentService")
    private IUnreadCommentService unreadCommentService;


    /**
     * 创建一条新的评论内容
     *
     * @param comment
     * @return
     */
    @Transactional
    public ResponseMessage createComment(Comment comment) {
        if (comment.getCommentId() > 0
                && comment.getCommentNick() != ""
                && comment.getArticleId() > 0
                && comment.getContent() != ""
                && comment.getCommentParentId() >= 0
                && comment.getCommentParentUserId() > 0
                && comment.getCommentParentUserNick() != ""
                && comment.getCommentedCount() >= 0){
            if (userService.hasUser(comment.getCommentId())
                    && userService.hasUser(comment.getCommentParentUserId())){
                if (articleService.hasArticle(comment.getArticleId())){
                    iCommentImpl.insertComment(comment);
                    /**
                     * 将相应的文章评论数+1
                     */
                    articleService.updateAddArticleCommentCount(comment.getArticleId());
                    /**
                     * 将被评论的评论数+1
                     */
                    iCommentImpl.addCommentedCountByCommentParentId(comment);
//                    将相应的未读评论表中的文章的未读条数加1
//                    unreadCommentService.updateUnreadCommentByArticleId(comment.getArticleId());
                    return responseMessageService.makeMessage(Message.SUCCESS);
                }
                return responseMessageService.makeMessage(Message.NO_HAVE_ARTICLE);

            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 查询评论信息
     *
     * @param articleId
     * @return
     */
    public ResponseMessage selectComment(int articleId, int start, int length) {
        if (articleId > 0 && start >= 0 && length >0){
            if (articleService.hasArticle(articleId)){
                List<Comment> commentList = iCommentImpl.getCommentListByArticleId(articleId, start, length);
                return responseMessageService.makeMessage(commentList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_ARTICLE);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过某个文章的id和被评论者的id评论
     *
     * @param articleId
     * @param commentParentId
     * @param start
     * @param length          @return List<Comment>
     */
    @Override
    public ResponseMessage selectCommentListByArticleIdAndCommentParentId(int articleId, int commentParentId, int start, int length) {
        if (articleId > 0 && commentParentId > 0 && start >= 0 && length >0){
            if (articleService.hasArticle(articleId)){
                List<Comment> commentList = iCommentImpl.getCommentListByArticleIdAndCommentParentId(articleId, commentParentId, start, length);
                return responseMessageService.makeMessage(commentList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_ARTICLE);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过某个文章的id和评论者的id获取评论
     *
     * @param articleId
     * @param commentId
     * @param start
     * @param length    @return List<Comment>
     */
    @Override
    public ResponseMessage selectCommentListByArticleIdAndCommentId(int articleId, int commentId, int start, int length) {
        if (articleId > 0 && commentId > 0 && start >= 0 && length >0){
            if (articleService.hasArticle(articleId) && articleService.hasArticle(commentId)){
                List<Comment> commentList = iCommentImpl.getCommentListByArticleIdAndCommentId(articleId, commentId, start, length);
                return responseMessageService.makeMessage(commentList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_ARTICLE);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }
}
